<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\WeekCheckout;
use Carbon\Carbon;
use GuzzleHttp\Client;
use App\Models\WeekCheckError;
use App\Handlers\WithdrawalHandler;


class WeekCheckoutTestJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $user;
    protected $week;


// 重新计算佣金 Start， 用到时候 ， 去运行就可以了
//Route::get('test1/{type?}/{job?}', function($type, $job = 9999){
//$t1 = (getStartOfWeek());
//$t2 = (getEndOfWeek(6));
//
//
//if ($type == 'delete') {
//$week_checkout = WeekProfitReturn::whereBetween('created_at', [$t1, $t2])->delete();
//$UserBalanceLog = UserBalanceLog::whereBetween('created_at', [$t1, $t2])
//->where('amount_type', 2)->where('type', 1)->delete();
//}
//
//if ($job == 'kaishi') {
//    $user = AdminUser::with(['week_checkout_one' => function($q) use ($t1, $t2){
//        $q->whereBetween('created_at', [$t1, $t2]);
//    }])->get();
//
//    foreach ($user as $k => $v){
//        if ($v->week_checkout_one){
//            dispatch(new WeekCheckoutTestJob($v, $v->week_checkout_one));
//        }
//    }
//}
// 重新计算佣金 End， 用到时候 ， 去运行就可以了

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($user, $week, $ttl = 1)
    {
        $this->user = $user;
        $this->week = $week;

        // 延时多久后在执行队列
        $this->delay($ttl);
        //
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        if (!$this->user->mt_login) {
            return;
        }
        //dd($this->week);

        $t1 = (strtotime(getStartOfWeek())) * 1000;
        $t2 = (strtotime(getEndOfWeek(6))) * 1000;

//        $t1 = 1603641600000;
//        $t2 = 1604073599000;


        if (!$this->user->mt_login) {
            $url = 'http://prod.bw.api.lwork.com/v1/api/trade/deal?serverId=4091&accountId=6070001&from='.$t1.'&to='.$t2;
        } else {
            $url = 'http://prod.bw.api.lwork.com/v1/api/trade/deal?serverId=4091&accountId='.$this->user->mt_login.'&from='.$t1.'&to='.$t2;
        }
        $http = new Client([
            'headers' => [
                'x-api-tenantId' => 'T002612',
                'x-api-token'    => 'Mk4p43N9gSBAebGr',
            ]
        ]);

        $result = json_decode($http->get($url)->getBody(true)->getContents(), true);

        if ($result['result']) {
            $this->week->profit          = collect($result['data'])->sum('profit') + collect($result['data'])->sum('swap');
            $this->week->profit_checkout = $this->week->profit > 0 ? $this->week->profit * ($this->user->profit_rate / 100) : 0;
            $this->week->volume          = collect($result['data'])->sum('volume');
            $this->week->all_data        = $result;


            // 实计收入多少
            $this->user->profit_checkout = $this->week->profit_checkout;

            //echo $this->user->name . '___' . $this->user->mt_login . '___' . $this->week->profit . '___' . $this->week->profit_checkout . "\n\n";
            if ($this->week->save()) {
                WithdrawalHandler::UserBalanceLog($this->user, '周数据汇总22');// 记录用户金额变化 周代理结算
//                if ($checkError){
//                    $checkError->status = 1;
//                    $checkError->save();
//                }
            }
        } else {
//            if (!$checkError){
//                $checkError = new WeekCheckError();
//                $checkError->admin_user()->associate($this->user);
//                $checkError->save();
//            }
            //dispatch(new WeekCheckoutTestJob($this->user, 1000));
        }
    }
}
